//
//  CallTranslationView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/4/18.
//

import UIKit
import Reusable
import Localize_Swift

extension CallTranslationView: NibLoadable {}

class CallTranslationView: UIView {
    
    // 我自己的
    @IBOutlet weak var selfMarkView: UIView!
    @IBOutlet weak var selfTitleView: UIView!
    @IBOutlet weak var selfTitleLabel: UILabel!
    @IBOutlet weak var selfTextView: UIView!
    @IBOutlet weak var selfTextLabel: UILabel!
    
    // 对方的
    @IBOutlet weak var peerMarkView: UIView!
    @IBOutlet weak var peerTitleView: UIView!
    @IBOutlet weak var peerTitleLabel: UILabel!
    @IBOutlet weak var peerTextView: UIView!
    @IBOutlet weak var peerTextLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        
        selfMarkView.isHidden = true
        peerMarkView.isHidden = true
        
        selfMarkView.backgroundColor = .clear
        peerMarkView.backgroundColor = .clear
        
        selfTitleView.addRoundedCorners(radius: 8)
        selfTextView.addRoundedCorners(radius: 8)
        
        peerTitleView.addRoundedCorners(radius: 8)
        peerTextView.addRoundedCorners(radius: 8)
        
        /// 监听语言的变化
        NotificationCenter.default.addObserver(self, selector: #selector(updateLanguageAction), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
        updateLanguageAction()
        
    }
    

    func updateContent(number: Int, hostId: String, model: IMRecvSeiModel?) {
        if number == 1 {
            // 没有连麦的人了
            DispatchQueue.main.async { [self] in
                
                guard let model = model else { return }
                let userName = LanguageManager.isLanguageZH ? model.username: model.usernameRu
                let text = NSMutableAttributedString(string: userName + ": " + model.text + "(\(model.trans))")
                text.color(.white)
                if !userName.isEmpty {
                    text.color(UIColor(hex: "#91E1F7"), range: text.string.inRange(of: userName).first!)
                }
                
                if hostId == String(UserProfile.userId) {
                    // 我是主播, 没有连麦的了, 只显示我自己的消息,别人的消息不展示
                    if model.userId != String(UserProfile.userId) {
                        return
                    }
                    // 我自己是主播,隐藏对方,显示自己,是我自己说的话
                    selfMarkView.isHidden = false
                    peerMarkView.isHidden = true
                    selfTextLabel.attributedText = text
                    
                } else {
                    // 我不是主播, 没有连麦的,只显示主播信息,不显示别人的信息
                    if model.userId == String(UserProfile.userId) {
                        return
                    }
                    selfMarkView.isHidden = true
                    peerMarkView.isHidden = false
                    peerTextLabel.attributedText = text
                }
            }
            return
        }
        
        self.model = model
    }
    
    
    
    var model: IMRecvSeiModel? {
        didSet {
            
            DispatchQueue.main.async { [self] in
               
                guard let model = model else { return }
                
                var userName = LanguageManager.isLanguageZH ? model.username: model.usernameRu
                if userName.isEmpty {
                    userName = "NULL"
                    if model.username.isEmpty && !model.usernameRu.isEmpty {
                        userName = model.usernameRu
                    }
                    
                    if !model.usernameRu.isEmpty && model.usernameRu.isEmpty {
                        userName = model.username
                    }
                }
                
                if userName == "NULL" && model.text.isEmpty {
                    return
                }
                
                
                let text = NSMutableAttributedString(string: userName + ": " + model.text + "(\(model.trans))")
                text.color(.white)
                text.color(UIColor(hex: "#91E1F7"), range: text.string.inRange(of: userName).first!)
                
                if model.userId == String(UserProfile.userId) {
                    // 我自己的
                    selfMarkView.isHidden = false
                    selfTextLabel.attributedText = text
                    
                } else {
                    // 对方的
                    peerMarkView.isHidden = false
                    peerTextLabel.attributedText = text
                }
            }

        }
    }
    
    // 更新语言
    @objc private func updateLanguageAction() {
        
        selfTitleLabel.text = "您说话看这里".localized(.call)
        peerTitleLabel.text = "对方说话看这里".localized(.call)
        
    }
    
}
